﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Runtime.InteropServices;


namespace DukeySoundHelper.API
{
    class WMICardGrab
    {

        [DllImport("kernel32")]
        private static extern void GetSystemInfo(ref CPUInfo cpuInfo);



        /// <summary> 
        /// 查询CPU的编号 
        /// </summary> 
        /// <returns></returns> 
        public static string GetCPUId()
        {
            ManagementClass mClass = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mClass.GetInstances();
            string strCPUId = null;
            foreach (ManagementObject mo in moc)
            {
                strCPUId = mo.Properties["ProcessorId"].Value.ToString();
                break;
            }
            return strCPUId;
        }


        public static string GetClientsysInfo()
        {
            try
            {
                string info = "";
                ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");

                ManagementObjectCollection queryCollection = query.Get();

                foreach (ManagementObject mo in queryCollection)
                {

                    string[] addresses = (string[])mo["IPAddress"];

                    string[] subnets = (string[])mo["IPSubnet"];

                    string[] defaultgateways = (string[])mo["DefaultIPGateway"];

                    string[] dns = (string[])mo["DNSServerSearchOrder"];

                    info = string.Format("Network Card: {0}<br>", mo["Description"]);

                    info += string.Format(" MAC Address: {0}<br>", mo["MACAddress"]);


                    foreach (string ipaddress in addresses)
                    {

                        info += string.Format(" IP Address: {0}<br>", ipaddress);

                    }

                    foreach (string subnet in subnets)
                    {

                        info += string.Format(" Subnet Mask: {0}<br>", subnet);

                    }

                    foreach (string defaultgateway in defaultgateways)
                    {
                        info += string.Format("defaultgateways: {0}<br>", defaultgateway);
                    }
                    foreach (string item in dns)
                    {
                        info += string.Format(" dns: {0}<br>", item);
                    }

                }
                return info;
            }
            catch
            {
                return "error!";
            }
        }
    }

    /// <summary> 
    /// 定义CPU的信息结构 
    /// LayoutKind.Automatic：为了提高效率允许运行态对类型成员重新排序 
    /// 注意：永远不要使用这个选项来调用不受管辖的动态链接库函数。 
    /// LayoutKind.Explicit：对每个域按照FieldOffset属性对类型成员排序 
    /// LayoutKind.Sequential：对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。 
    /// </summary> 
    [StructLayout(LayoutKind.Sequential)]
    public struct CPUInfo
    {
        /// <summary> 
        /// OEM ID 
        /// </summary> 
        public uint dwOenId;

        /// <summary> 
        /// 页面大小 
        /// </summary> 
        public uint dwPageSize;
        public uint lpMinimumApplicationAddress;
        public uint lpMaximumApplicationAddress;
        public uint dwActiveProcessorMask;

        /// <summary> 
        /// CPU个数 
        /// </summary> 
        public uint dwNumberOfProcessors;

        /// <summary> 
        /// CPU类型 
        /// </summary> 
        public uint dwProcessorType;
        public uint dwAllocationGranularity;

        /// <summary> 
        /// CPU等级 
        /// </summary> 
        public uint dwProcessorLevel;
        public uint dwProcessorRevision;
    } 
}